package com.sky.controller.admin;

import com.sky.dto.SetmealDTO;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.entity.Setmeal;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.SetmealService;
import com.sky.vo.SetmealVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.models.auth.In;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/admin/setmeal")
@Api(tags = "套餐管理接口")
public class SetmealController {

    @Autowired
    SetmealService setmealService;

    @Autowired
    RedisTemplate redisTemplate;

    /**
     * 分页查询
     *
     * @param setmealPageQueryDTO
     * @return
     */
    @ApiOperation("套餐分页查询")
    @GetMapping("/page")
    public Result<PageResult> pageSetmeal(SetmealPageQueryDTO setmealPageQueryDTO) {
        PageResult pageResult = setmealService.pageSetmeal(setmealPageQueryDTO);
        return Result.success(pageResult);
    }


    /**
     * 添加套餐
     *
     * @param setmealDTO
     * @return
     */
    @ApiOperation("添加套餐")
    @PostMapping
    public Result addSetmeal(@RequestBody SetmealDTO setmealDTO) {
        setmealService.addSetmeal(setmealDTO);
        return Result.success();
    }


    /**
     * 根据id查询套餐
     *
     * @param id
     * @return
     */
    @ApiOperation("根据id查询套餐")
    @GetMapping("/{id}")
    public Result<SetmealVO> findById(@PathVariable Long id) {
        SetmealVO setmealVO = setmealService.findById(id);
        return Result.success(setmealVO);
    }


    /**
     * 修改套餐
     *
     * @param setmealDTO
     * @return
     */
    @ApiOperation("修改套餐")
    @PutMapping
    public Result updateSetmeal(@RequestBody SetmealDTO setmealDTO) {
        // 清楚修改的套餐中分类的缓存
        Long categoryId = setmealDTO.getCategoryId();
        String SETMEAL_UPDATE = "SETMEAL_" + categoryId;
        redisTemplate.delete(SETMEAL_UPDATE);

        setmealService.updateSetmeal(setmealDTO);
        return Result.success();
    }


    /**
     * 根据ids删除套餐
     *
     * @param ids
     * @return
     */
    @ApiOperation("根据id删除套餐")
    @DeleteMapping
    public Result delByIds(@RequestParam("ids") List<Long> ids) {
        // 清缓存 只能删除停售的套餐,停售的套餐不在小程序中显示,如果改状态,在改状态的时候就已经清空缓存了
        setmealService.delByIds(ids);
        return Result.success();
    }

    /**
     * 修改状态
     *
     * @param status
     * @param id
     * @return
     */
    @ApiOperation("修改状态")
    @PostMapping("/status/{status}")
    public Result updateStatus(@PathVariable Integer status, Long id) {
        setmealService.updateStatus(status, id);
        // 获取修改状态的这个套餐，然后通过套餐的类型进行清缓存事务
        SetmealVO setmealVO = setmealService.findById(id);
        Long categoryId = setmealVO.getCategoryId();
        String SETMEAL_UPDATE = "SETMEAL_" + categoryId;
        redisTemplate.delete(SETMEAL_UPDATE);
        return Result.success();
    }


}
